using LitJson;
using System;
using UnityEngine;
using UnityEngine.UI;

namespace BToolkit
{
    public class LicenceCreater : MonoBehaviour
    {
        private const string publicKey =
            @"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyroeJpu1M5byXqSblSAz
/G1G42CTEwJkRzkddZdrgxeXdZa8SEJqYBDnlI4XtXDlWmBmCuxGiUwpzqTSrr2P
3by9ij8bjbo8tCHFx+zM8nX1LNbiqW0KwmWKGxnIM4isKlhQQeNSAlVV0FvQNuJu
F3dhY7vbOBdLNaXq+zhPthAsvgZwfsWX1fHxbm9RtYP9z26KfFJJ6tpFepeWBxED
xVP9eLL0xnbUYmyaEwZ0NcCVb8I2OT5EG1QMRBi0/KBv6ET2QAcChvoeWm+pW7D2
w0O4AL+vEKmBebiyHMX5OtsAKoOhE/b+fbaYlq9XQDPJcDsvGoBFZumphq0nsC6I
kwIDAQAB
-----END PUBLIC KEY-----";

        private const string privateKey =
            @"-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDKuh4mm7UzlvJe
pJuVIDP8bUbjYJMTAmRHOR11l2uDF5d1lrxIQmpgEOeUjhe1cOVaYGYK7EaJTCnO
pNKuvY/dvL2KPxuNujy0IcXH7MzydfUs1uKpbQrCZYobGcgziKwqWFBB41ICVVXQ
W9A24m4Xd2Fju9s4F0s1per7OE+2ECy+BnB+xZfV8fFub1G1g/3Pbop8Uknq2kV6
l5YHEQPFU/14svTGdtRibJoTBnQ1wJVvwjY5PkQbVAxEGLT8oG/oRPZABwKG+h5a
b6lbsPbDQ7gAv68QqYF5uLIcxfk62wAqg6ET9v59tpiWr1dAM8lwOy8agEVm6amG
rSewLoiTAgMBAAECggEBAKQg6PYzDBIXfGR5emjSqKQOyNyfvJMgVZWAPUSN8gIx
YHNYNVl/Q+ZA6WzRWYqRO27fKt5fCoBwBE0EDnpgiJBqFT5O93zYMftYeGrzY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/L1I9PN47cFpeKBHA5CJ7CxzdU0miOi
KF4948QiIe1COvmqO4imksIW4A==
-----END PRIVATE KEY-----";

        public InputField inputDeviceID, inputYear, inputMonth, inputDay;
        public BButton btnConfirm, btnCopy;
        public Text textLicence;
        private RSAUtils rsa;

        string licence;

        private void Start()
        {
            textLicence.text = "";
            btnCopy.gameObject.SetActive(false);

            inputYear.text = DateTime.Now.ToString("yyyy");
            inputMonth.text = DateTime.Now.ToString("MM");
            inputDay.text = DateTime.Now.ToString("dd");

            rsa = new RSAUtils(privateKey, publicKey);

            btnConfirm.onClick.AddListener(() =>
            {
                if (string.IsNullOrEmpty(inputDeviceID.text))
                {
                    DialogAlert.Show("请输入设备码");
                    return;
                }
                if (inputDeviceID.text.Length != 32)
                {
                    DialogAlert.Show("设备码位数不对");
                    return;
                }
                if (string.IsNullOrEmpty(inputYear.text))
                {
                    DialogAlert.Show("请输入截止年份");
                    return;
                }
                if (string.IsNullOrEmpty(inputMonth.text))
                {
                    DialogAlert.Show("请输入截止月份");
                    return;
                }
                if (string.IsNullOrEmpty(inputDay.text))
                {
                    DialogAlert.Show("请输入截止日");
                    return;
                }

                JsonData data = new JsonData();
                data["deviceId"] = inputDeviceID.text;
                data["year"] = int.Parse(inputYear.text);
                data["month"] = int.Parse(inputMonth.text);
                data["day"] = int.Parse(inputDay.text);
                string json = JsonMapper.ToJson(data);
                licence = rsa.PrivateKeyEncrypt(json);
                textLicence.text = $"激活码:\n\n{licence}";
                btnCopy.gameObject.SetActive(true);
            });

            btnCopy.onClick.AddListener(() =>
            {
                CopyAndPaste.Copy(licence);
                DialogAlert.Show("复制成功");
            });
        }
    }
}